/*
 * Checkstyle4nb is a plugin for netbeans for checking Java coding conventions.
 * Copyright (C) 2006-2007, Andrea Matsunaga
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.ammatsun.checkstyle4nb.actions;

import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.Repository;

import java.io.IOException;
import java.io.OutputStream;

import java.util.Properties;

/**
 * Manages the checkstyle module settings.
 *
 * @version    1.0, 2006/Jul/12
 * @author     Andrea Matsunaga
 */
public class CheckstyleSettings extends Properties {
    /** Property that stores permanently the checkstyle configuration file. */
    public static final String CHECKSTYLE_CONFIG = "checkstyle.config";
    /** Property that stores permanently the properties file for checkstyle. */
    public static final String CHECKSTYLE_PROPERTIES = "checkstyle.properties";

    /** File object to the checkstyle settings stored by netbeans. */
    private FileObject settingsFile;

    /** Creates a new instance of CheckstyleSettings. */
    public CheckstyleSettings() {
        // Tries to access the "Settings" folder
        FileObject settingsFolder = Repository.getDefault().getDefaultFileSystem().getRoot().getFileObject("Settings");
        if (null == settingsFolder) {
            // If it does not exist, create the folder
            try {
                settingsFolder = Repository.getDefault().getDefaultFileSystem().getRoot().createFolder("Settings");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        // Tries to access the file with checkstyle setting "Checkstyle.Cfg"
        settingsFile = settingsFolder.getFileObject("Checkstyle", "Cfg");
        try {
            // If it does not exist, create the file
            if (null == settingsFile) {
                settingsFile = settingsFolder.createData("Checkstyle", "Cfg");
            }
        } catch (IOException e) {
            // File can not be created; for now simply output the error
            e.printStackTrace();
        }
        // Reload the values from the file
        reloadSettings();
    }

    /**
     * Store permanently the checkstyle settings.
     * @return true if settings were successfully saved, false otherwise
     */
    public boolean storeSettings() {
        FileLock lock = null;

        try {
            lock = settingsFile.lock();
            OutputStream os = settingsFile.getOutputStream(lock);
            this.store(os, "Checkstyle configuration for Netbeans module.");
            os.close();
        } catch (IOException e) {
            // File can not be locked or written to
            // For now simply output the error
            e.printStackTrace();
            return false;
        } finally {
            lock.releaseLock();
        }
        return true;
    }

    /**
     * Reload the settings from the permanent storage.
     */
    public void reloadSettings() {
        try {
            load(settingsFile.getInputStream());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
